package com.qili.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qili.core.utils.DictTypes;
import com.qili.core.utils.DictUtil;
import com.qili.entity.base.BaseDict;
import com.qili.entity.supervisor.SupervisorFile;
import com.qili.entity.supervisor.SupervisorFileTemp;
import com.qili.entity.supervisor.dto.SupervisorFileDTO;
import com.qili.entity.sys.SysUser;
import com.qili.mapper.it.ItFileMapper;
import com.qili.mapper.supervisor.SupervisorFileMapper;
import com.qili.service.SysUserService;
import com.qili.service.it.ItFileService;
import com.qili.service.supervisor.SupervisorFileService;
import com.qili.service.supervisor.SupervisorFileTempService;
import com.qili.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author zhaoyongke
 * @date 2021/5/26
 * @description  最佳实践库统计
 */

@RequestMapping("/search/decision")
@Controller
public class BestPracticeController {

    @Autowired
    SupervisorFileService supervisorFileService;
    @Autowired
    SupervisorFileMapper supervisorFileMapper;
    @Autowired
    UploadUtil uploadUtil;
    @Autowired
    SupervisorFileTempService tempService;
    @Autowired
    ItFileService itFileService;
    @Autowired
    ItFileMapper itFileMapper;
    @Autowired
    SysUserService sysUser;
    // 最佳实践库统计
    @GetMapping("BestPracticeIndex")
    @RequiresPermissions("zn:jc:zjsjk")
    public String BestPracticeIndex(Model model) {
        return "/decision/BestPractice/BestPracticeIndex";
    }

    @GetMapping("fileDetailList")
    public String fileDetailList(Model model) {
        return "/decision/BestPractice/fileDetailList";
    }

    /**
    * @Author:zhaoyongke
    * @Description:  最佳实践库---  成果类型统计
    * @param dto
    * @param model
    * @param page
    * @param limit
    * @Date:13:35 2021/5/26
    */
    @ApiOperation(value = "/showcgList", httpMethod = "GET", notes = "展示成果类型统计")
    @GetMapping(value = "/showcgList")
    @ResponseBody
    public ReType showcgList(SupervisorFileDTO dto, Model model, String page, String limit) {
        // 2022年5月24日15:53:44 重构 [之前版本]
//        return this.showcgListAgo(dto,model,page,limit);
        ReType  re=new ReType();
        PageHelper.startPage(1, 100000);
        // 查询最佳实践库
        SupervisorFile  filesu=new SupervisorFile();
        filesu.setFileName("最佳实践库");
        filesu.setZskfileType("1");
        filesu.setUrl(uploadUtil.getUploadPath()+"最佳实践库");
        List<SupervisorFile> select = itFileMapper.selectFile(filesu);
        if(select.size()>0){
            SupervisorFile supervisorFile = select.get(0);
            String id = supervisorFile.getId();
            // 根据父id查询所有儿子
            List<SupervisorFile> childs = itFileMapper.selectChildsByParentId(id);
            // 获取儿子id拼接ids
            String ids = itFileMapper.selectIdPj(id, "1");
            // 根据ids获取数量
            List<Map<String, String>> numList = itFileMapper.getNumByIds(ids);
            // 创建集合
            List<Map> listMap = new ArrayList<>();
            int  ordernum=0;
            for (SupervisorFile child : childs) {
                // 封装结果数据
                Map<String, Object> map = new HashMap<>();
                map.put("id", child.getId());
                map.put("fileName", child.getFileName());
                map.put("num","0");
                for (Map<String, String> numMap : numList) {
                    if (numMap.containsKey("tempName") && numMap.get("tempName").equals(child.getId())){
                        // 对应的数据
                        String num1 = String.valueOf(numMap.get("num"));
                        map.put("num",num1);
                        break;
                    }
                }
                ordernum++;
                map.put("ordernum",ordernum);
                // 设置子级数据
                getChilds(child.getId(),map);
                listMap.add(map);
                // 从map中获取 children
                List<Map<String, Object>> children = (List<Map<String, Object>>) map.get("children");
                for (Map<String, Object> stringObjectMap : children) {
                    listMap.add(stringObjectMap);
                }
            }
            re.setPageNum(Long.valueOf(page));
            re.setCount(listMap.size());
            re.setData(listMap);
        }
        return re;
    }

    /**
     * 获取子级数据
     * @param parentId 父id
     * @param map 结果集
     * @return
     */
    private void getChilds(String parentId, Map<String, Object> map){
        List<Map<String, Object>> resultList = new ArrayList<>();
        // 根据父id查询所有儿子
        List<SupervisorFile> childs = itFileMapper.selectChildsByParentId(parentId);
        // 获取儿子id拼接ids
        String ids = itFileMapper.selectIdPj(parentId, "1");
        if (StringUtils.isNotBlank(ids)){
            // 根据ids获取数量
            List<Map<String, String>> numList = itFileMapper.getNumByIds(ids);
            if (!CollectionUtils.isEmpty(childs)){
                for (SupervisorFile child : childs) {
                    // 创建Map
                    Map<String, Object> resultMap = new HashMap<>();
                    resultMap.put("id", child.getId());
                    resultMap.put("num","0");
                    for (Map<String, String> numMap : numList) {
                        if (numMap.containsKey("tempName") && numMap.get("tempName").equals(child.getId())){
                            // 对应的数据
                            String num1 = String.valueOf(numMap.get("num"));
                            resultMap.put("num",num1);
                            break;
                        }
                    }
                    StringBuffer space = new StringBuffer();
                    space.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");//这里是空格
                    resultMap.put("fileName", space.toString()+child.getFileName());
                    resultList.add(resultMap);
                }
            }
        }
        map.put("children", resultList);
    };

    /**
     * 2022年5月24日15:54:00 重构
     * @param dto
     * @param model
     * @param page
     * @param limit
     * @return
     */
    private ReType showcgListAgo(SupervisorFileDTO dto, Model model, String page, String limit){
        ReType  re=new ReType();
        PageHelper.startPage(1, 100000);
        Map paraMap = new HashMap();
        // 文件夹
        SupervisorFile file = new SupervisorFile();
        file.setZskfileType("1");
        List<SupervisorFile> sysMenus = itFileMapper.selectFile(file);
        // 文件
        SupervisorFile filell = new SupervisorFile();
        List<SupervisorFile> sysMenusfile = itFileMapper.selectFile(filell);
//        SupervisorFile filell = new SupervisorFile();
//        List<SupervisorFile> sysMenusfile = supervisorFileService.select(filell);
        SupervisorFile  filesu=new SupervisorFile();
        filesu.setFileName("最佳实践库");
        filesu.setZskfileType("1");
        filesu.setUrl(uploadUtil.getUploadPath()+"最佳实践库");
        List<SupervisorFile> select = itFileMapper.selectFile(filesu);
        if(select.size()>0){
            SupervisorFile supervisorFile = select.get(0);
            String id = supervisorFile.getId();

            SupervisorFile  filechild=new SupervisorFile();
            filechild.setZskfileType("1");
            filechild.setSuperiorId(id);
            List<SupervisorFile> supers =   itFileMapper.selectFile(filechild);
            int pNum = 1000;
            JSONArray jsonArr = new JSONArray();
            List<Map> listMap = new ArrayList<>();
            int  ordernum=0;
            for (SupervisorFile menu : supers) {
                Map map1 = new HashMap();
                map1.put("id", menu.getId());
                Map<String, Integer> mapnum = new HashMap<>();
                mapnum.put("num", 0);
                getChildsFile(menu, mapnum, sysMenusfile);
                Integer num1 = mapnum.get("num");
                map1.put("fileName", menu.getFileName());
                map1.put("num",num1);
                ordernum++;
                map1.put("ordernum",ordernum);
                listMap.add(map1);
                map1.put("children", getChilds(menu, sysMenus, sysMenusfile,listMap));
            }
            System.out.println(listMap.toString());
            re.setPageNum(Long.valueOf(page));
            re.setCount(listMap.size());
            re.setData(listMap);
        }
        return re;
    }

    // 获取子节点
    public List<Map> getChilds(SupervisorFile menu, List<SupervisorFile> menuList, List<SupervisorFile> sysMenusfile,  List<Map>  listMap ) {
        List<Map> mapsList = new ArrayList<>();

        for (SupervisorFile menus : menuList) {
            if (menu.getId().equals(menus.getSuperiorId())) {
                StringBuffer space = new StringBuffer();
                Map map = new HashMap();
                map.put("id", menus.getId());
                // 获取文件的数量
                List<Map> m = getChilds(menus,  menuList, sysMenusfile,listMap);
                Map<String, Integer> mapnum = new HashMap<>();
                mapnum.put("num", 0);
                getChildsFile(menus, mapnum, sysMenusfile);
                Integer num1 = mapnum.get("num");
                space.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");//这里是空格
                map.put("fileName", space.toString()+menus.getFileName());
                map.put("num",  num1);
                if (m != null && m.size() > 0) {
                    map.put("children", m);
                }
                if (map != null && map.size() > 0) {
                    listMap.add(map);
                    mapsList.add(map);
                }
            }
        }
        return mapsList;
    }

    public void getChildsFile(SupervisorFile menu, Map<String, Integer> map, List<SupervisorFile> sysMenusfile) {
        for (SupervisorFile menus : sysMenusfile) {
            if (menu.getId().equals(menus.getSuperiorId())) {
                if ("2".equals(menus.getZskfileType())  && !"2".equals(menus.getDeleteState())   ) {
                    int ss = map.get("num") + 1;
                    map.put("num", ss);
                }
                // 获取文件的数量
                getChildsFile(menus, map, sysMenusfile);

            }
        }
    }


    // 最佳实践库 按照  人员统计
    @ApiOperation(value = "/showFileByUserZj", httpMethod = "GET", notes = "展示成果类型统计")
    @GetMapping(value = "/showFileByUserZj")
    @ResponseBody
    public ReType showFileByUserZj(SupervisorFileDTO dto, Model model, String page, String limit) {
        ReType re = new ReType();
        String createUsername = dto.getCreateUsername();
        Page<SupervisorFileDTO> tPage = PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
        List<SupervisorFileDTO> data = supervisorFileService.showFileByUserZj(dto);
        return new ReType(tPage.getTotal(),data);
    }


    /**
    * @Author:zhaoyongke
    * @Description: 最佳实践库文档统计----部门统计
    * @param dto
    * @param model
    * @param page
    * @param limit
    * @Date:13:20 2021/5/27
    */
    @ApiOperation(value = "/showFileByDeptZj", httpMethod = "GET", notes = "展示成果类型统计")
    @GetMapping(value = "/showFileByDeptZj")
    @ResponseBody
    public ReType showFileByDeptZj(SupervisorFileDTO dto, Model model, int page, int limit) {
        ReType re = new ReType();
        Page<BaseDict> tPage = PageHelper.startPage(page, limit);
        List<SupervisorFileDTO> list=selectBmtj();
        return new ReType(tPage.getTotal(), list);

    }
    public List<SupervisorFileDTO> selectBmtj(){
        List<BaseDict> dictByType = DictUtil.getDictByType(DictTypes.DEPT);
        List<SupervisorFileDTO> list=new ArrayList<>();
        String types="103,104,105,102,1111";//1111是查全部的
        for (int i = 0; i <dictByType.size() ; i++) {
            BaseDict baseDict = dictByType.get(i);
            SupervisorFileDTO sup=new SupervisorFileDTO();
            SupervisorFileDTO sup1=new SupervisorFileDTO();
            sup.setDeptCode(baseDict.getBadtCode());
            sup.setDeptName(baseDict.getBadtName());
            String[] arr=types.split(",");
            for(int j=0;j<arr.length;j++){
                sup.setType(arr[j]);

                SupervisorFileDTO list1=supervisorFileMapper.showFileDeptTypeNum(sup);
                sup1.setDeptName(baseDict.getBadtName());
                sup1.setId(baseDict.getBadtCode());
                if("103".equals(arr[j])){
                    sup1.setNum1(list1.getNum());
                }else if("104".equals(arr[j])){
                    sup1.setNum2(list1.getNum());
                }else if("105".equals(arr[j])){
                    sup1.setNum3(list1.getNum());
                }else if("102".equals(arr[j])){
                    sup1.setNum4(list1.getNum());
                }else if("1111".equals(arr[j])){
                    sup1.setNum(list1.getNum());
                }
            }
            list.add(sup1);
        }
        return list;
    }
//
//    select  *  from  base_dict   where badt_typecode='BM001'

    /**
     * 实践库文档统计  人员统计 文档列表跳转
     * @return
     */
    @GetMapping(value = "/bastPracticeFile")
    public String bastPracticeFile(Model model,String id,String types,String deptCode,String cgType,String name,String deptName){
        model.addAttribute("id",id);
        model.addAttribute("types",types);
        model.addAttribute("deptCode",deptCode);
        model.addAttribute("cgType",cgType);
        model.addAttribute("name",name);
        model.addAttribute("deptName",deptName);
      return "/decision/BestPractice/BastPracticeFile";
    }

    /**
     * 实践库文档统计 文档列表查询
     * @param model
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/selBastPracticeFile")
    public ReType SelectBastPracticeFile(HttpServletRequest request,Model model, int page, int limit){
        SupervisorFileDTO sup=new SupervisorFileDTO();
        String id=request.getParameter("id");
        String types=request.getParameter("types");
        String deptCode=request.getParameter("deptCode");
        String fileName=request.getParameter("fileName");
        String cgType=request.getParameter("cgType");
        String name=request.getParameter("name");
        String deptName = request.getParameter("deptName");
        model.addAttribute("id",id);
        sup.setUserId(id);
        sup.setDeptCode(deptCode);
        sup.setType(types);
        sup.setFileName(fileName);
        sup.setContent(name);
        sup.setDeptName(deptName);
        if(StringUtils.isNotBlank(cgType)&&"cg".equals(cgType)){
            Page<SupervisorFileDTO> tPage = PageHelper.startPage(page, 100000);
            // 文件
//            SupervisorFile filell = new SupervisorFile();
//            List<SupervisorFile> sysMenusfile = supervisorFileService.select(filell);
            // 备注2022年5月24日17:00:58 修改为根据id查询所有文件
//            Example example = new Example(SupervisorFile.class);
//            example.and().andNotEqualTo("deleteState","2");
//            List<SupervisorFile> sysMenusfile = supervisorFileService.selectByExample(example);
            List<SupervisorFile> list = itFileMapper.selectFilesByParentId(id,"2");
//            List<SupervisorFile> list = new ArrayList<>();
//            getChildsFiles(id,list,sysMenusfile);
            //文档名筛选
            if(StringUtils.isNotBlank(fileName)) {
                List<SupervisorFile> list1=new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    int j=list.get(i).getFileName().indexOf(fileName,0);
                    if(j!=-1){
                        list1.add(list.get(i));
                    }
                }
                return new ReType(list1.size(), list1);
            }
            return new ReType(list.size(), list);
        }
        List<SupervisorFileDTO> list=supervisorFileMapper.selBastPracticeFile(sup);
        String type ="";
        for(SupervisorFileDTO dto:list){
            if (!StringUtils.isBlank(dto.getFileType())){
                type = itFileService.getFileTypeByFileName(dto.getFileType());
            }else{
                String fileNames = dto.getFileName();
                if(StringUtils.isNotBlank(fileName) && fileName.contains(".")){
                    type = itFileService.getFileTypeByFileName(fileNames);
                }
            }
            //图片、视频url做处理
            if("1".equals(type)||"2".equals(type)){
                String imgUrl = dto.getUrl();
                dto.setUrl(url(imgUrl));
            }
        }
        return new ReType(list.size(), list);
    }
    //图片的url
    public String url(String url){
        String imgUrl = url;
        String uploadPath = uploadUtil.getUploadPath();
        imgUrl = imgUrl.substring(uploadPath.length());
        imgUrl = File.separator+"images"+File.separator+imgUrl;
        return imgUrl;
    }
    public void getChildsFiles(String id, List<SupervisorFile> list, List<SupervisorFile> sysMenusfile) {
        for (SupervisorFile menus : sysMenusfile) {
            if (id.equals(menus.getSuperiorId())) {
                if ("2".equals(menus.getZskfileType())) {
                    list.add(menus);
                }
                // 获取文件的数量
                getChildsFiles(menus.getId(), list, sysMenusfile);

            }
        }
    }
    /**
     * 导出excel
     * @param request
     * @param response
     */
    @RequestMapping("exportTable")
    public void exportTable(HttpServletRequest request, HttpServletResponse response){
        String types=request.getParameter("types");
        List<SupervisorFileDTO> data =new ArrayList<>();
        String  fileName="";
        HashMap map=new HashMap();
        String fpId="";
        if("1".equals(types)){
            // 文件夹
            SupervisorFile file = new SupervisorFile();
            file.setZskfileType("1");
            List<SupervisorFile> sysMenus = supervisorFileService.select(file);
            // 文件
            SupervisorFile filell = new SupervisorFile();
            List<SupervisorFile> sysMenusfile = supervisorFileService.select(filell);
            SupervisorFile  filesu=new SupervisorFile();
            filesu.setFileName("最佳实践库");
            filesu.setZskfileType("1");
            filesu.setUrl(uploadUtil.getUploadPath()+"最佳实践库");
            List<SupervisorFile> select = supervisorFileService.select(filesu);
            if(select.size()>0) {
                SupervisorFile supervisorFile = select.get(0);
                String id = supervisorFile.getId();

                SupervisorFile filechild = new SupervisorFile();
                filechild.setZskfileType("1");
                filechild.setSuperiorId(id);
                List<SupervisorFile> supers = supervisorFileService.select(filechild);
                int pNum = 1000;
                JSONArray jsonArr = new JSONArray();
                List<Map> listMap = new ArrayList<>();
                int ordernum = 0;
                for (SupervisorFile menu : supers) {
                    Map map1 = new HashMap();
                    map1.put("id", menu.getId());
                    Map<String, Integer> mapnum = new HashMap<>();
                    mapnum.put("num", 0);
                    getChildsFile(menu, mapnum, sysMenusfile);
                    Integer num1 = mapnum.get("num");
                    map1.put("fileName", menu.getFileName());
                    map1.put("num", num1);
                    ordernum++;
                    map1.put("ordernum", ordernum);
                    listMap.add(map1);
                    map1.put("children", getChildss(menu, sysMenus, sysMenusfile, listMap));
                }
                map.put("lists",listMap);
            }
            map.put("name","成果类型统计");
            fpId="87004";
            fileName="最佳实践库文档_按成果类型统计_"+ DateUtil.getCurrentDateNoSpliter();
        }else if("2".equals(types)) {//人员统计
            fileName="最佳实践库文档_按人员统计_"+ DateUtil.getCurrentDateNoSpliter();
            SupervisorFileDTO dto = new SupervisorFileDTO();
            dto.setCreateUsername(request.getParameter("createUsername"));
            data = supervisorFileService.showFileByUserZj(dto);
            for(int i=0;i<data.size();i++){
                data.get(i).setOrderCode((i+1)+"");
            }
            map.put("lists",data);
            map.put("name","人员统计");
            fpId="87002";
        }else if("3".equals(types)){//部门统计
            fileName="最佳实践库文档_按部门统计_"+ DateUtil.getCurrentDateNoSpliter();
            List<SupervisorFileDTO> list=selectBmtj();
            for(int i=0;i<list.size();i++){
                list.get(i).setOrderCode((i+1)+"");
            }
            map.put("lists",list);
            map.put("name","部门统计");
            fpId="87003";
        }
        try {
            ExcelExportUtil.exportExcel(response,map,fileName,fpId);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *预览excel
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("viewTable")
    public JsonUtil viewTable(HttpServletRequest request, HttpServletResponse response){
        JsonUtil result = new JsonUtil();
        try{
            String types=request.getParameter("types");
            List<SupervisorFileDTO> data =new ArrayList<>();
            String  fileName="";
            HashMap map=new HashMap();
            String fpId="";
            File formExcel =null;
            if("1".equals(types)){
                // 文件夹
                SupervisorFile file = new SupervisorFile();
                file.setZskfileType("1");
                List<SupervisorFile> sysMenus = supervisorFileService.select(file);
                // 文件
                SupervisorFile filell = new SupervisorFile();
                List<SupervisorFile> sysMenusfile = supervisorFileService.select(filell);
                SupervisorFile  filesu=new SupervisorFile();
                filesu.setFileName("最佳实践库");
                filesu.setZskfileType("1");
                filesu.setUrl(uploadUtil.getUploadPath()+"最佳实践库");
                List<SupervisorFile> select = supervisorFileService.select(filesu);
                if(select.size()>0) {
                    SupervisorFile supervisorFile = select.get(0);
                    String id = supervisorFile.getId();

                    SupervisorFile filechild = new SupervisorFile();
                    filechild.setZskfileType("1");
                    filechild.setSuperiorId(id);
                    List<SupervisorFile> supers = supervisorFileService.select(filechild);
                    int pNum = 1000;
                    JSONArray jsonArr = new JSONArray();
                    List<Map> listMap = new ArrayList<>();
                    int ordernum = 0;
                    for (SupervisorFile menu : supers) {
                        Map map1 = new HashMap();
                        map1.put("id", menu.getId());
                        Map<String, Integer> mapnum = new HashMap<>();
                        mapnum.put("num", 0);
                        getChildsFile(menu, mapnum, sysMenusfile);
                        Integer num1 = mapnum.get("num");
                        map1.put("fileName", menu.getFileName());
                        map1.put("num", num1);
                        ordernum++;
                        map1.put("ordernum", ordernum);
                        listMap.add(map1);
                        map1.put("children", getChildss(menu, sysMenus, sysMenusfile, listMap));
                    }
                    map.put("lists",listMap);
                }
                map.put("name","成果类型统计");
                fpId="87004";
                fileName="最佳实践库文档_按成果类型统计_"+ DateUtil.getCurrentDateNoSpliter();
                formExcel = createFormExcel(fpId, fileName, map);
            }else if("2".equals(types)) {//人员统计
                fileName="最佳实践库文档_按人员统计_"+ DateUtil.getCurrentDateNoSpliter();
                SupervisorFileDTO dto = new SupervisorFileDTO();
                dto.setCreateUsername(request.getParameter("createUsername"));
                data = supervisorFileService.showFileByUserZj(dto);
                for(int i=0;i<data.size();i++){
                    data.get(i).setOrderCode((i+1)+"");
                }
                map.put("lists",data);
                map.put("name","人员统计");
                fpId="87002";
                formExcel = createFormExcel(fpId, fileName, map);
            }else if("3".equals(types)){//部门统计
                fileName="最佳实践库文档_按部门统计_"+ DateUtil.getCurrentDateNoSpliter();
                List<SupervisorFileDTO> list=selectBmtj();
                for(int i=0;i<list.size();i++){
                    list.get(i).setOrderCode((i+1)+"");
                }
                map.put("lists",list);
                map.put("name","部门统计");
                fpId="87003";
                formExcel = createFormExcel(fpId, fileName, map);
            }
            String tempFilePath = uploadUtil.getTempPath()+File.separator+ new Date().getTime()
                    +File.separator+fileName+formExcel.getName().substring(formExcel.getName().lastIndexOf("."));
            File tempFile = new File(tempFilePath);
            if(!tempFile.getParentFile().exists()){
                tempFile.getParentFile().mkdirs();
            }
            tempFile.createNewFile();
            FileUtils.copyFile(formExcel,tempFile);
            formExcel.delete();
            SupervisorFileTemp temp = new SupervisorFileTemp();
            temp.setUrl(tempFile.getPath());
            temp.setFileName(tempFile.getName());
            temp.setFileType(tempFile.getName().substring(tempFile.getName().lastIndexOf(".")));
            temp.setType("tz");
            tempService.insertSelective(temp);
            result.setFlag(true);
            result.setData(temp.getId());
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    // 获取子节点
    public List<Map> getChildss(SupervisorFile menu, List<SupervisorFile> menuList, List<SupervisorFile> sysMenusfile,  List<Map>  listMap ) {
        List<Map> mapsList = new ArrayList<>();

        for (SupervisorFile menus : menuList) {
            if (menu.getId().equals(menus.getSuperiorId())) {
                Map map = new HashMap();
                map.put("id", menus.getId());
                // 获取文件的数量
                List<Map> m = getChilds(menus,  menuList, sysMenusfile,listMap);
                Map<String, Integer> mapnum = new HashMap<>();
                mapnum.put("num", 0);
                getChildsFile(menus, mapnum, sysMenusfile);
                Integer num1 = mapnum.get("num");
                map.put("fileName","                "+menus.getFileName());
                map.put("num",  num1);
                if (m != null && m.size() > 0) {
                    map.put("children", m);
                }
                if (map != null && map.size() > 0) {
                    listMap.add(map);
                    mapsList.add(map);
                }
            }
        }
        return mapsList;
    }
    /**
     * 创建表单临时表格文件
     * @param fileName
     * @param dataMap
     * @return
     */
    public File createFormExcel(String ptid,String fileName,HashMap dataMap){
        try {
            // 获取模板信息
            String srcFilePath = FilePathUtil.getPathConfigTemplateFile(ptid);
            //生成的临时导出文件
            File destFile  = File.createTempFile(fileName, ".xls");
            //  开始转换。利用  transformer 转到Excel
            XLSTransformer transformer = new XLSTransformer();
            // 参数：srcFilePath：模板源文件    cMap：需要导出的数据   destFile.getAbsolutePath()：下载的目标文件
            transformer.transformXLS(srcFilePath, dataMap, destFile.getAbsolutePath());
            return destFile;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
        return null;
    }
}
